package com.demo2do.fms.admin.service.authority.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.fms.admin.dao.authority.ResourceDao;
import com.demo2do.fms.admin.dao.authority.RoleDao;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.authority.ResourceService;
import com.demo2do.fms.entity.authority.Resource;
import com.demo2do.fms.entity.authority.ResourceType;
import com.demo2do.fms.entity.authority.Role;
import com.demo2do.fms.entity.authority.RoleTemplate;
import com.demo2do.fms.entity.authority.User;

/**
 * 
 * @author Ysy
 *
 */
@Service("resourceService")
public class ResourceServiceImpl extends DataServiceImpl<Resource> implements ResourceService {

    @Dao
    private ResourceDao resourceDao;
    
    @Dao
    private RoleDao roleDao;
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.ResourceService#count(com.demo2do.fms.entity.authority.RoleTemplate)
     */
    public int count(RoleTemplate template) {
        
        if(template != null) {
            return resourceDao.count(template);
        }
        
        return 0;
    }

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.ResourceService#list(com.demo2do.fms.entity.authority.RoleTemplate, com.demo2do.core.entity.page.Page)
     */
    public List<Resource> list(RoleTemplate template, Page page) {
        List<Resource> result = resourceDao.list(template, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.ResourceService#list(com.demo2do.fms.entity.authority.RoleTemplate, com.demo2do.fms.entity.authority.ResourceType)
     */
    public List<String> list(RoleTemplate template, ResourceType type) {
        return resourceDao.list(template, type);
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.ResourceService#count(com.demo2do.fms.entity.authority.Role)
     */
    public int count(Role role) {
        return resourceDao.count(role.getCode());
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.ResourceService#list(com.demo2do.fms.entity.authority.Role, com.demo2do.core.entity.page.Page)
     */
    public List<Resource> list(Role role, Page page) {
        
        List<Resource> result = resourceDao.list(role.getCode(), page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        
        return result;
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.ResourceService#count(com.demo2do.fms.entity.authority.User)
     */
    public int count(User user) {
        return resourceDao.count(user.getAuthority());
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.ResourceService#list(com.demo2do.fms.entity.authority.User, com.demo2do.core.entity.page.Page)
     */
    public List<Resource> list(User user, Page page) {
        List<Resource> result = resourceDao.list(user.getAuthority(), page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.ResourceService#list(com.demo2do.fms.entity.authority.User)
     */
    @SuppressWarnings("unchecked")
    public Map<String, List<String>> list(User user) {
        
        Map<String, List<String>> result = new HashMap<String, List<String>>();
        
        List<Resource> resources = resourceDao.list(user.getAuthority());
        
        Map<String, List<Resource>> resourcesMap = CollectionUtils.constructMapValues(resources, "type");
        
        for(Entry<String, List<Resource>> item : resourcesMap.entrySet()){
            List<String> names = CollectionUtils.extractPropertyList(item.getValue(), "name");
            result.put(item.getKey(), names);
        }
            
        return result;
    }
    
}
